<?php
require_once 'common/config.php';
require_once 'common/functions.php';
// 检查登录状态
check_login();
$db = db_connect();
// 获取分页参数
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
// 获取搜索参数
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$where = '';
$params = [];
if ($search) {
    $where = " WHERE title LIKE :search OR category LIKE :search";
    $params[':search'] = "%{$search}%";
}
// 获取总记录数
$sql = "SELECT COUNT(*) FROM fitness_guides" . $where;
$stmt = $db->prepare($sql);
if ($search) {
    $stmt->bindValue(':search', "%{$search}%");
}
$stmt->execute();
$total = $stmt->fetchColumn();
$total_pages = ceil($total / $limit);
// 获取健身指南列表
$sql = "SELECT * FROM fitness_guides" . $where . " ORDER BY id DESC LIMIT :offset, :limit";
$stmt = $db->prepare($sql);
if ($search) {
    $stmt->bindValue(':search', "%{$search}%");
}
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
$guides = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>健身指南管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>健身指南管理</h2>
                </div>
                <div class="header-right">
                    <a href="fitness_guide_add.php" class="btn btn-primary">
                        <i class="fas fa-plus"></i> 添加指南
                    </a>
                </div>
            </header>

            <div class="content">
                <!-- 搜索和筛选 -->
                <div class="filter-bar">
                    <form action="" method="GET" id="searchForm" class="search-bar">
                        <div class="search-input">
                            <input type="text" name="search" placeholder="搜索指南标题、内容..." 
                                   value="<?php echo htmlspecialchars($search); ?>">
                        </div>
                        <button type="submit" class="btn-search">
                            <i class="fas fa-search"></i>
                        </button>
                    </form>
                </div>

                <!-- 数据表格 -->
                <div class="data-table">
                    <table>
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>封面图片</th>
                                <th>标题</th>
                                <th>分类</th>
                                <th>浏览量</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($guides as $guide): ?>
                            <tr>
                                <td><?php echo $guide['id']; ?></td>
                                <td>
                                    <?php if ($guide['cover_image']): ?>
                                    <img src="<?php echo '../api/user/' . htmlspecialchars($guide['cover_image']); ?>" 
                                         class="avatar-small" 
                                         alt="封面图片" 
                                         style="max-width: 100px; height: 60px; object-fit: cover; border-radius: 4px;"
                                         onclick="showImage(this.src)">
                                    <?php else: ?>
                                    <i class="fas fa-image"></i>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo htmlspecialchars($guide['title']); ?></td>
                                <td><?php echo htmlspecialchars($guide['category']); ?></td>
                                <td><?php echo $guide['views']; ?></td>
                                <td>
                                    <span class="badge <?php echo $guide['status'] == 1 ? 'badge-success' : 'badge-danger'; ?>">
                                        <?php echo $guide['status'] == 1 ? '启用' : '禁用'; ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="fitness_guide_edit.php?id=<?php echo $guide['id']; ?>" class="btn-icon" title="编辑">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="javascript:void(0)" onclick="deleteGuide(<?php echo $guide['id']; ?>)" 
                                       class="btn-icon text-danger" title="删除">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <?php if ($total_pages > 1): ?>
                <div class="pagination">
                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <a href="?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>" 
                           class="<?php echo $page == $i ? 'active' : ''; ?>">
                            <?php echo $i; ?>
                        </a>
                    <?php endfor; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- 图片预览模态框 -->
    <div id="imageModal" class="modal" onclick="this.style.display='none'">
        <img id="modalImage" class="modal-content">
    </div>

    <script>
    function showImage(src) {
        const modal = document.getElementById('imageModal');
        const modalImg = document.getElementById('modalImage');
        modal.style.display = "block";
        modalImg.src = src;
    }

    function deleteGuide(id) {
        if (confirm('确定要删除这个健身指南吗？')) {
            window.location.href = `fitness_guide_delete.php?id=${id}`;
        }
    }
    </script>

    <style>
    .modal {
        display: none;
        position: fixed;
        z-index: 1000;
        padding-top: 50px;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0,0,0,0.9);
        cursor: pointer;
    }

    .modal-content {
        margin: auto;
        display: block;
        max-width: 80%;
        max-height: 80%;
    }

    .avatar-small {
        cursor: pointer;
        transition: transform 0.2s;
    }

    .avatar-small:hover {
        transform: scale(1.05);
    }

    .filter-bar {
        background: #fff;
        padding: 10px;
        border-radius: 4px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.05);
        margin-bottom: 20px;
    }

    .search-bar {
        display: flex;
        margin-top: 12px;
        align-items: center;
        gap: 8px;
    }

    .search-input {
        flex: 1;
    }

    .search-input input {
        width: 100%;
        padding: 6px 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        font-size: 14px;
        line-height: 1.5;
        background: #f8f9fa;
        transition: all 0.3s;
    }

    .search-input input:focus {
        border-color: #3498db;
        background: #fff;
        outline: none;
    }

    .btn-search {
        padding: 6px 16px;
        background: #3498db;
        color: #fff;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        line-height: 1.5;
        transition: background 0.3s;
    }

    .btn-search:hover {
        background: #2980b9;
    }

    /* 响应式布局 */
    @media (max-width: 768px) {
        .search-bar {
            flex-direction: column;
        }
        
        .search-input,
        .btn-search {
            width: 100%;
        }
    }
    </style>
</body>
</html> 